package cn.kgc.service.impl;

import cn.kgc.common.redisutils.stringRedisTemplateUtil;
import cn.kgc.dao.mapper.ItripUserLinkUserMapper;
import cn.kgc.dtos.Dto;
import cn.kgc.pojo.ItripUserLinkUser;
import cn.kgc.service.linkUserService;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Service;
import cn.kgc.common.dtoutils.dtoUtil;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class linkUserServiceimpl implements linkUserService {

    @Resource
    private ItripUserLinkUserMapper itripUserLinkUserMapper;

    @Resource
    private stringRedisTemplateUtil stringRedisTemplateUtil;
    /**
     * 查询方法
     * @param name
     * @return
     */
    @Override
    public Dto selectByname(String name,String token) {
        try{
            ItripUserLinkUser itr= JSONObject.parseObject(stringRedisTemplateUtil.getKey(token),ItripUserLinkUser.class);
            List<ItripUserLinkUser> list= itripUserLinkUserMapper.selectByPrimaryKey(name,(int)itr.getId());
            if (list==null){
                return  dtoUtil.getDto("1001","没有数据",null,"false",null);
            }
            return dtoUtil.getDto("1000","查询成功",list,"true",null);
        }catch (Exception e){
            return  dtoUtil.getDto("1002","查询异常",null,"false",e);
        }


    }

    /**
     * 添加方法
     * @param itripUserLinkUser
     * @return
     */
    @Override
    public Dto insertlinkUser(ItripUserLinkUser itripUserLinkUser,String token) {
        try {
            ItripUserLinkUser itr= JSONObject.parseObject(stringRedisTemplateUtil.getKey(token),ItripUserLinkUser.class);
              itripUserLinkUser.setUserId(itr.getId());
            itripUserLinkUser.setCreationDate(new Date());
             int num= itripUserLinkUserMapper.insert(itripUserLinkUser);
             if (num>0){
                 return dtoUtil.getDto("2000","修改成功",null,"true",null);
             }
             return  dtoUtil.getDto("2001","添加异常",null,"false",null);
        }catch (Exception e){
            return dtoUtil.getDto("2002","添加异常",null,"false",null);
        }
    }

    /**
     * 修改方法
     * @param itripUserLinkUser
     * @return
     */
    @Override
    public Dto updatelinkUser(ItripUserLinkUser itripUserLinkUser,String token) {
        try{
            ItripUserLinkUser itr= JSONObject.parseObject(stringRedisTemplateUtil.getKey(token),ItripUserLinkUser.class);
            itripUserLinkUser.setUserId(itr.getId());
            itripUserLinkUser.setModifyDate(new Date());
            int num=itripUserLinkUserMapper.updateByPrimaryKey(itripUserLinkUser);
            if (num>0){
                return  dtoUtil.getDto("3000","修改成功",null,"true",null);
            }
            return   dtoUtil.getDto("3001","修改失败",null,"false",null);
        }catch (Exception e){
            return   dtoUtil.getDto("3002","修改异常",null,"false",null);
        }

    }

    /**
     * 删除方法
     * @param id
     * @return
     */
    @Override
    public Dto deletelinkUser(int [] id) {
        try{
            int num=0;
            for (int i=0;i<id.length;i++){
              num+=itripUserLinkUserMapper.deleteByPrimaryKey(Long.valueOf(id[i]));
            }
            if (num>0){
                return dtoUtil.getDto("4000","删除成功",null,"true",null);
            }
            return dtoUtil.getDto("4001","删除失败",null,"false",null);
        }catch (Exception e){
            return  dtoUtil.getDto("4002","出现异常",null,"false",null);
        }

    }
}
